package com.tju.backend.plugin.t_io_server;

import com.tju.backend.plugin.common.Const;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;
import org.tio.core.intf.Packet;
import org.tio.server.intf.ServerAioListener;

/**
 * t-io监听器
 *
 * @USER: CLS
 */
@Slf4j
@Component
public class SPServerAioListener implements ServerAioListener {

    public static ChannelContext channelContext = null;

    /**
     * @author cls
     */
    @Override
    public void onAfterConnected(ChannelContext channelContext, boolean isConnected, boolean isReconnect) throws Exception {
        if (isConnected) {
            log.info("{客户端连接进来了，共【" + Tio.getAll(channelContext.tioConfig).getObj().size() + "】人在线" + "'}");
            Tio.bindGroup(channelContext, Const.GROUP_NAME);
            SPServerAioListener.channelContext = channelContext;
        }
    }

    /**
     * @author tanyaowu
     */
    @Override
    public void onAfterSent(ChannelContext channelContext, Packet packet, boolean isSentSuccess) throws Exception {
    }

    @Override
    public void onBeforeClose(ChannelContext channelContext, Throwable throwable, String remark, boolean isRemove) {
    }

    /**
     * @author tanyaowu
     */
    @Override
    public void onAfterDecoded(ChannelContext channelContext, Packet packet, int packetSize) throws Exception {
    }

    /**
     * @author tanyaowu
     */
    @Override
    public void onAfterReceivedBytes(ChannelContext channelContext, int receivedBytes) throws Exception {
    }

    /**
     * @author tanyaowu
     */
    @Override
    public void onAfterHandled(ChannelContext channelContext, Packet packet, long cost) throws Exception {
    }

    /**
     * 设置心跳是否超时
     * true 一直接收心跳不超时，false为自定义心跳是否超时
     *
     * @param channelContext
     * @param interval
     * @param heartbeatTimeoutCount
     * @return
     */
    @Override
    public boolean onHeartbeatTimeout(ChannelContext channelContext, Long interval, int heartbeatTimeoutCount) {
        return true;
    }
}
